﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using SeniorCapstoneProject.ServiceReference2;
using SeniorCapstoneProject.ServiceReference3;
using System.IO;

namespace SeniorCapstoneProject
{
    delegate void CheckValueInvoker(int gID);
    
    public partial class frmLogin : Form
    {
        private login ClientLogin;

        public List<FileInformation> fims { get; set; }
        public verifyUser vUser { get; set; }
        public int uGroupID { get; set; }
        public frmLogin()
        {
            InitializeComponent();
            if(!string.IsNullOrEmpty(Properties.Settings.Default.Username))
            {
                txtUsername.Text = Properties.Settings.Default.Username;
                chkSaveUsername.Checked = true;
            }
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            tssStatus.Text = "";
            tssStatus.Text = "Connecting to server";
            UseWaitCursor = true;
            if (chkSaveUsername.Checked)
            {
                Properties.Settings.Default.Username = txtUsername.Text;
            }
            else
            {
                Properties.Settings.Default.Username = "";
            }
            Properties.Settings.Default.Save();
            
            string password = txtPassword.Text;
            verifyUser uLogin = new verifyUser();
            uLogin.Username = txtUsername.Text;
            uLogin.Password = txtPassword.Text;
            this.vUser = uLogin;

            login ClientLogin = new login(vUser.Username, vUser.Password, this);
            Thread StartClientLogin = new Thread(new ThreadStart(ClientLogin.clientLogin));
            StartClientLogin.Name = "ClientLogin";
            StartClientLogin.IsBackground = true;
            StartClientLogin.Start(); //Start thread
            this.ClientLogin = ClientLogin;
        }

        private void frmLogin_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (this.DialogResult != DialogResult.OK)
            {
                CustomMessage cMessage = new CustomMessage();
                cMessage.lblTitle.Text = "Leaving?";
                cMessage.lblTitle.Visible = true;
                cMessage.lblMessage.Text = "Are you sure you want to exit?";
                cMessage.lblMessage.Visible = true;
                cMessage.Text = "Leaving";
                cMessage.ShowDialog();
                if (cMessage.DialogResult == DialogResult.Yes)
                {
                    e.Cancel = false;
                }
                else
                {
                    e.Cancel = true;
                }
            }
        }

        private void txtPassword_TextChanged(object sender, EventArgs e)
        {
            tssStatus.Text = "";
        }

        private void txtUsername_TextChanged(object sender, EventArgs e)
        {
            tssStatus.Text = "";
        }

        public void CheckValue(int uGroup)
        {
            if(uGroup != -1)
            {
                this.uGroupID = ClientLogin.GroupID;

                if (uGroupID != 0)
                {
                    UseWaitCursor = false;
                    this.DialogResult = DialogResult.OK;
                    this.Close();
                }
                else
                {
                    tssStatus.Text = "Invalid username or password";
                    UseWaitCursor = false;
                }
            }
            else
            {
                tssStatus.Text = "Unable to contact server";
                UseWaitCursor = false;
            }
        }

        private void frmLogin_Load(object sender, EventArgs e)
        {

        }
    }

    public class login
    {
        private string uname = null;
        private string password = null;
        private frmLogin frml = null;

        private int gID;
        public int GroupID
        {
            get
            {
                return gID;
            }
            set
            {
                gID = value;
            }
        }



        private List<FileInformation> fileInfoReturn;

        public List<FileInformation> FileInfoReturn
        {
            get
            {
                return fileInfoReturn;
            }
            set
            {
                fileInfoReturn = value;
            }
        }

        public login(string uname, string password, frmLogin frml)
        {
            this.uname = uname;
            this.password = password;
            this.frml = frml;
        }

        public void clientLogin()
        {
            Thread.Sleep(500);
            try
            {
                TransferFilesClient tfc = new TransferFilesClient();

                GroupID = tfc.AuthUser(uname, password);

                frml.Invoke(new CheckValueInvoker(frml.CheckValue), GroupID);
            }
            catch (Exception ex)
            {
                errorReport(ex.Message.ToString());
                frml.Invoke(new CheckValueInvoker(frml.CheckValue), -1);
            }
        }

        public void errorReport(string ex)
        {
            string activeDir = Environment.SpecialFolder.MyDocuments.ToString();
            string newPath = Path.Combine(activeDir, "Hermes Company Transfer");

            Directory.CreateDirectory(newPath);

            newPath = Path.Combine(newPath, "HCT error report.txt");
            if (!File.Exists(newPath))
            {
                File.Create(newPath);
            }

            using (StreamWriter sw = File.AppendText(newPath))
            {
                sw.WriteLine(DateTime.Now + " --error--> " + ex);
                sw.Close();
            }

            CustomMessage cMessage = new CustomMessage();
            cMessage.lblTitle.Text = "Error!!!";
            cMessage.lblTitle.Visible = true;
            cMessage.lblMessage.Text = "An error has been recorded and saved in " + newPath;
            cMessage.lblMessage.Visible = true;
            cMessage.btnNo.Visible = false;
            cMessage.btnYes.Text = "Ok";
            cMessage.Text = "Error!!!";
            cMessage.StartPosition = FormStartPosition.CenterScreen;
            cMessage.ShowDialog();
        }
    }
}
